package cn.mengfly.fxtest.app;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mengfly
 */
public class NoNullComboApp extends Application {

    private final List<String> countries = new ArrayList<>();
    private final Map<String, List<String>> citiesMap = new LinkedHashMap<>();

    private final ComboBox<String> country = new ComboBox<>();
    private final ComboBox<String> city = new ComboBox<>();

    private void initData() {
        String countryFr = "France";
        String countryDe = "Germany";
        String countryCh = "Switzerland";

        countries.add(countryFr);
        countries.add(countryDe);
        countries.add(countryCh);

        List<String> frenchCities = new ArrayList<>();
        frenchCities.add("Paris");
        frenchCities.add("Strasbourg");

        List<String> germanCities = new ArrayList<>();
        germanCities.add("Berlin");
        germanCities.add("Cologne");
        germanCities.add("Munich");

        List<String> swissCities = new ArrayList<>();
        swissCities.add("Zurich");

        citiesMap.put(countryFr, frenchCities);
        citiesMap.put(countryDe, germanCities);
        citiesMap.put(countryCh, swissCities);

        country.getItems().add("");
        country.getItems().addAll(countries);
        // empty selection is object and not null
        country.setValue("");

        city.getItems().add("");
        city.setValue("");
    }

    @Override
    public void start(Stage primaryStage) {
        Label countryLabel = new Label("Country:");
        country.setPrefWidth(200.0d);
        Label cityLabel = new Label("City:");
        city.setPrefWidth(200.0d);
        Button saveButton = new Button("Save");

        VBox vbox = new VBox(
                countryLabel,
                country,
                cityLabel,
                city,
                saveButton
        );
        vbox.setAlignment(Pos.CENTER_LEFT);
        vbox.setSpacing(10.0d);

        TilePane outerBox = new TilePane(vbox);
        outerBox.setAlignment(Pos.CENTER);

        country.setOnAction(event -> {
            String value = country.getValue();
            city.getItems().removeIf(s -> !s.isEmpty());

            if (citiesMap.containsKey(value)) {
                city.getItems().addAll(citiesMap.get(value));
            }
        });

        saveButton.setOnAction(event -> System.out.println("saving country '" + country.getValue()
                + "', city='" + city.getValue() + "'"));

        Scene scene = new Scene(outerBox);
        initData();

        primaryStage.setScene(scene);
        primaryStage.setTitle("NoNullComboApp");
        primaryStage.setWidth(320);
        primaryStage.setHeight(480);
        primaryStage.show();
    }
}
